package com.yanqu.road.server.cmd.systemgroup.newmonopolymkt.office;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.CrossAppointCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.CrossNewMonopolyMarketGroup;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.CrossNewMonopolyMarketMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.C_CROSS_NEW_MONOPOLY_MARKET_OFFICE_ACTION, desc = "进入大宋榷署 抢夺/撤回位置")
public class CrossMonopolyMarketOfficeActionCmd extends CrossAppointCommand {

    private long userId;
    private int posIndex;
    private int seatTime;
    private long seatUserId;
    private int type;
    private byte decorationClass;
    private int decorationOverTime;
    private UserBaseInfo userBaseInfo;
    private String consume;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketOfficeActionCrossReqMsg req = NewMonopolyMarketProto.NewMonopolyMarketOfficeActionCrossReqMsg.parseFrom(packet.getBytes());
        posIndex = req.getCtsMsg().getPosIndex();
        seatTime = req.getCtsMsg().getSeatTime();
        seatUserId = req.getCtsMsg().getSeatUserId();
        type = req.getCtsMsg().getType();
        userId = packet.getPlayerId();
        if(req.hasPlayerInfo()){
            decorationClass = (byte)(req.getDecorationClass());
            decorationOverTime = req.getDecorationOverTime();
            userBaseInfo = PlayerBasePb.parseToUserBaseInfo(req.getPlayerInfo());
        }
        if(req.hasConsume()){
            consume = req.getConsume();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if(userBaseInfo != null){
            CrossUserMgr.updateUserBaseInfo(userId, userBaseInfo);
        }
        CrossNewMonopolyMarketGroup monopolyMarket = CrossNewMonopolyMarketMgr.getMarket(groupId);
        if(monopolyMarket != null){
            NewMonopolyMarketProto.NewMonopolyMarketOfficeActionToGameServerMsg.Builder builder = monopolyMarket.getMarketOffice().action(type, posIndex, seatTime, seatUserId, userId,
                    decorationClass, decorationOverTime, userBaseInfo);
            if(!StringUtils.isNullOrEmpty(consume)){
                builder.setConsume(consume);
            }
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_NEW_MONOPOLY_MARKET_OFFICE_ACTION_FROM_CROSS, builder));
        }
    }

    @Override
    public void sendData() {

    }
}
